//
//  XYNetWorkTool.m
//  RenrenMiju
//
//  Created by Xy on 15/12/1.
//  Copyright © 2015年 Xy. All rights reserved.
//

#import "XYNetWorkTool.h"

@implementation XYNetWorkTool

+ (void)getWithUrl:(NSString *)url parameter:(NSDictionary *)parameter httpHeader:(NSDictionary *)header responseType:(ResponseType)responseType success:(SuccessBlock)success fail:(FailBlock)fail {


//    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
//    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
//    [SVProgressHUD showWithStatus:@"正在加载"];
//    if ([XYReachabilityManager sharedReachabilityManager].currentReachabilityStatus == NotReachable) {
//        [SVProgressHUD showErrorWithStatus:@"请检查网络"];
//        return;
//    }
//    
//    if ([XYReachabilityManager sharedReachabilityManager].currentReachabilityStatus == ReachableViaWWAN) {
//        [SVProgressHUD showInfoWithStatus:@"您正处于非WIFI状态下"];
//    }
    
    
    //1.初始化sessionManager
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //2.处理请求头
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    //3.判断返回值数据类型
    switch (responseType) {
        case ResponseTypeDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case ResponseTypeJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case ResponseTypeXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
    }
    //4.判断返回值所接受的具体类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    //5.get请求
    [manager GET:url parameters:parameter success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        if (success) {
            success(responseObject);
//            [SVProgressHUD popActivity];


        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (fail) {
            fail(error);
//            [SVProgressHUD dismiss];
//            [SVProgressHUD showErrorWithStatus:@"网络请求失败"];
        }
    }];
}

+ (void)postWithUrl:(NSString *)url body:(id)body bodyType:(BodyType)bodyType httpHeader:(NSDictionary *)header responseType:(ResponseType)responseType success:(SuccessBlock)success fail:(FailBlock)fail {
//    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
//    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
//    [SVProgressHUD showWithStatus:@"正在加载"];
//    
//    if ([XYReachabilityManager sharedReachabilityManager].currentReachabilityStatus == NotReachable) {
//        [SVProgressHUD showErrorWithStatus:@"请检查网络"];
//        return;
//    }
//    
//    if ([XYReachabilityManager sharedReachabilityManager].currentReachabilityStatus == ReachableViaWWAN) {
//        [SVProgressHUD showInfoWithStatus:@"您正处于非WIFI状态下"];
//    }
    
    //1.初始化
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //2.处理Body
    switch (bodyType) {
        case BodyTypeDictionary:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case BodyTypeString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
                return parameters;
            }];
            break;
    }
    //3.处理请求头
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    //4.判断返回值数据类型
    switch (responseType) {
        case ResponseTypeDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case ResponseTypeJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case ResponseTypeXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
    }
    //5.判断返回值所接受的具体类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    //6.psot请求
    [manager POST:url parameters:body success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        if (success) {
            success(responseObject);
//            [SVProgressHUD popActivity];

        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (fail) {
            fail(error);
//            [SVProgressHUD dismiss];
//            [SVProgressHUD showErrorWithStatus:@"请求服务器失败"];

        }
    }];
}

+ (CGSize)heightForCellWithText:(NSString *)text Size:(CGSize)size fontSize:(NSInteger)fontSize {
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil];
        return rect.size;
}




@end
